package com.simple.card.aspect;

import com.google.gson.Gson;
import com.simple.card.Utils.AesUtil;
import com.simple.card.VO.ResultUserInfoVO;
import com.simple.card.annotation.LoginCheck;
import com.simple.card.dataobject.KabaoUser;
import com.simple.card.dto.KaBaoUserDTO;
import com.simple.card.enums.ResultEnum;
import com.simple.card.exception.CreditException;
import com.simple.card.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@Aspect
@Component
@Slf4j
public class LoginAuthAspect {

    @Autowired
    private UserService userService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Around("execution(public * com.simple.card.controller.*.*(..)) && @annotation(loginCheck))")
    public Object doVerify(ProceedingJoinPoint point, LoginCheck loginCheck){
        try {
            boolean login = loginCheck.login();
            if(login) {
                //获取header头中的       token值
                ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
                HttpServletRequest request = attributes.getRequest();
                String token = request.getHeader("Access-User-Token");
                if(StringUtils.isEmpty(token)) {
                    throw new CreditException(ResultEnum.CHECK_ACCESS_USER_TOKEN_IS_EMPTY);
                }
                //aes解秘
                String access_user_token = AesUtil.decryptData(token);
                if(StringUtils.isEmpty(access_user_token)) {
                    throw new CreditException(ResultEnum.CHECK_ACCESS_USER_TOKEN_IS_EMPTY);
                }
                //根据token获取用户信息
                KabaoUser kabaoUser = userService.findByToken(access_user_token);
                if(kabaoUser == null) {
                    throw new CreditException(ResultEnum.CHECK_ACCESS_USER_IS_EMPTY);
                }
                //校验用户登陆时间是否过期
                if(kabaoUser.getStatus() != 1 && new Date().getTime()/1000 > kabaoUser.getTimeout().getTime()/1000) {
                    throw new CreditException(ResultEnum.CHECK_ACCESS_USER_TIMEOUT);
                }
            }
            return point.proceed();
        }catch (Throwable throwable) {
            if (throwable instanceof CreditException){
                throw new CreditException(((CreditException) throwable).getCode() ,throwable.getMessage());
            }
            return null;
        }
    }
}
